package com.yenlien.traceyun.control.controller;

import com.alibaba.fastjson.JSONObject;
import com.yenlien.traceyun.control.config.Log;
import com.yenlien.traceyun.control.entity.Report;
import com.yenlien.traceyun.control.service.ReportService;
import com.yenlien.traceyun.webase.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 投诉举报
 */
@RestController
@RequestMapping("/report")
public class ReportController extends BaseController {

    @Autowired
    private ReportService reportService;

    /**
     * 新增投诉
     *
     * @param params
     * @return
     */
    @PostMapping("/save")
    public Object save(@RequestBody JSONObject params) {
        Report report = params.getJSONObject("report").toJavaObject(Report.class);
        reportService.save(report);
        return success("success");
    }

    /**
     * 查询投诉
     *
     * @param params
     * @return
     */
    @PostMapping("/inquire")
    public Object inquire(@RequestBody JSONObject params) {
        Integer page = params.getInteger("page");
        if (page > 0) {
            Integer page1 = page - 1;
            return buildPage(reportService.select(page1));
        } else {
            return fail("页面加载错误");
        }
    }

    /**
     * 搜索栏(模糊查询)
     *
     * @param params
     * @return
     */
    @PostMapping("/search")
    @Log(operation = "举报投诉", profession = "搜索举报")
    public Object search(@RequestBody Report report) {
        return success(reportService.findByList(report));
    }

    /**
     * 根据id查询授权
     *
     * @param params
     * @return
     */
    @PostMapping("/selectId")
    @Log(operation = "举报投诉", profession = "查看举报")
    public Object selectId(@RequestBody JSONObject params) {
        String id = params.getString("id");
        if (id.length() > 0) {
            return success(reportService.selectId(id));
        } else {
            return fail("授权不存在");
        }
    }

    /**
     * 根据id修改授权
     *
     * @param params
     * @return
     */
    @PostMapping("/update")
    @Log(operation = "举报投诉", profession = "修改举报")
    public Object update(@RequestBody JSONObject params) {
        Report report = params.getJSONObject("report").toJavaObject(Report.class);
        reportService.update(report);
        return success("success");
    }


}
